<?php

namespace App\Http\Controllers\Home;

use App\models\Bale;
use App\models\Cart;
use App\models\Category;
use App\models\Collect;
use App\models\Goods;
use App\models\HotWords;
use App\models\Slider;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class HomeController extends Controller
{
//    前台首页
    public function index()
    {
        $hotWords = HotWords::orderBy('number','DESC')->limit(7)->get();
        $sliders = Slider::latest()->limit(4)->get();
//        dd($sliders);
        $goods = Goods::where('is_commend',1)->latest()->limit(6)->get();
        $carts = Cart::where('user_id',auth()->id())->latest()->get();
//        先获取到顶级分类数据
       $oneCategory = Category::where('parent_id',0)->get();
        $hotGoods = Goods::orderBy('collect_num','DESC')->limit(6)->get();
       return view('home.index',compact('oneCategory','carts','goods','hotGoods','sliders','hotWords'));
    }
//   前台列表页
    public function lists(category $category)
    {
//        dd($category['id']);
        $hotWords = HotWords::orderBy('number','DESC')->limit(7)->get();
        $carts = Cart::where('user_id',auth()->id())->get();
        $goods = $category->getCategoryGood();
//        dd($goods);
//        找出当前分类的所有子分类
        $sonCategory = Category::where('parent_id',$category['id'])->get();
//        获取当前分类的所有父级分类
        $fatherCategory = $category->getFatherCategory();
        $commendGoods = Goods::where('is_commend',1)->latest()->limit(4)->get();
        $likeGoods = Goods::limit(6)->get();
//        dd($likeGoods);
//        foreach ($goods as $good){
//            $collects = Collect::where('user_id',auth()->id())->where('good_id',$good->id)->first();
//        }
//        dd($collects);
        return view('home.lists',compact('goods','sonCategory','fatherCategory','commendGoods','likeGoods','carts','collects','hotWords'));
    }
//    前台内容页
    public function content(Goods $good,$bale = 0)
    {
//        dd($good);
        $hotWords = HotWords::orderBy('number','DESC')->limit(7)->get();
        $carts = Cart::where('user_id',auth()->id())->get();
//        找到该商品的分类id
        $category = Category::find($good['category_id'][2]);
//        dd($category);
//        找到该商品分类下的所有父级分类
        $fatherCategory = $category->getFatherCategory();
//        获取该分类下的某个货品
       if ($bale)
       {
           $huopin = Bale::find($bale);
       }else {
           $huopin = Bale::where('goods_id',$good['id'])->first();
       }
//       $kucun = $good->bale->stock;
//       dd($kucun);
        $commendGoods = Goods::where('is_commend',1)->latest()->limit(4)->get();
//       热门商品是通过获取商品表中收藏次数的多少来获取的
        $hotGoods = Goods::orderBy('collect_num','DESC')->limit(6)->get();
        return view('home.content',compact('good','fatherCategory','huopin','commendGoods','hotGoods','carts','hotWords'));
    }
    public function search($hotWord = 0)
    {
//       dd(123);
//        dd($request->input('search'));
        $hotWords = HotWords::orderBy('number','DESC')->limit(7)->get();
        $status = HotWords::where('hotwords',$hotWord)->first();
        if ($status)
        {
            $status['number'] = $status['number'] + 1;
            $status->update();
        }else{
            HotWords::create(['hotwords'=>$hotWord,'number'=>1]);
        }
        $carts = Cart::where('user_id',auth()->id())->get();
        $commendGoods = Goods::where('is_commend',1)->latest()->limit(4)->get();
        $likeGoods = Goods::get()->random(6);
        if (!$hotWord)
        {
//            如果没有输入搜索内容，随机取八条商品数据传到模板展示
            $goods = Goods::get()->random(8);
        }else{
            $goods = Goods::search($hotWord)->get();
//            dd($goods->isEmpty());
            if($goods->isEmpty()){
                $goods = Goods::get()->random(8);
//                dd(111);
            }

        }
//        dd($goods);
        return view('home.search',compact('goods','commendGoods','likeGoods','carts','hotWords'));
//        dd($goods);
    }
}
